package com.tanhua.api.impl.db;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.api.db.SoulScoreApi;
import com.tanhua.domain.db.SoulScore;
import com.tanhua.mapper.db.SoulScoreMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.print.Paper;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Richard Wang
 * @date 2021/9/5 15:21
 */
@Service
public class SoulScoreImpl implements SoulScoreApi {


    @Autowired
    private SoulScoreMapper soulScoreMapper;

    @Override
    public Boolean isNew(Long userId, Integer paperId) {
        QueryWrapper<SoulScore> qr = new QueryWrapper<>();
        qr.eq("user_id", userId);
        qr.eq("paper_id", paperId);
        SoulScore soulScore = soulScoreMapper.selectOne(qr);
        return soulScore == null ? true : false;
    }

    @Override
    public void insert(Long id, Integer pagerId, Integer score) {
        SoulScore soulScore = new SoulScore();
        soulScore.setUserId(id.intValue());
        soulScore.setPaperId(pagerId);
        soulScore.setScore(score);
        soulScoreMapper.insert(soulScore);
    }

    @Override
    public void update(Long userId, Integer paperId, Integer score) {
        //查找到旧的记录
        QueryWrapper<SoulScore> qr = new QueryWrapper<>();
        qr.eq("user_id", userId);
        qr.eq("paper_id", paperId);
        SoulScore soulScore = soulScoreMapper.selectOne(qr);
        //删除旧的
        soulScoreMapper.deleteById(soulScore.getId());
        //增加新的
        insert(userId, paperId, score);
    }

    @Override
    public List<Integer> findSimilarId(Integer score, Integer paperId) {
        List<Integer> idList = new ArrayList<>();
        QueryWrapper<SoulScore> qr = new QueryWrapper<>();
        //查看在同一等级的试卷下,得分与当前用户相差5分的用户
        qr.eq("paper_id", paperId);
        //todo:是否正确
        qr.between("score", score - 5, score + 5);
        List<SoulScore> scoreList = soulScoreMapper.selectList(qr);
        for (SoulScore soulScore : scoreList) {
            idList.add(soulScore.getUserId());
        }
        return idList;
    }


}
